var Note = window.Note || {};
(function(){
	/**
	 *
	 *	Note Draw Class
	 *
	 *	@namespace Note.Draw
	 * 	@class Draw
	 * 	@constructor
	 *
	 */
	Note.Draw = function(){};
	/**
	 * Member Variable
	 */
	//画笔样式
	Note.Draw.prototype._penStyle = "#000";
	//画笔当前线条的宽度
	Note.Draw.prototype._penWidth = 2;
	
	/**
	 *	函数名: drawShape
	 *	功能: 绘制图形
	 *	@param context {Object} 绘图句柄
	 *	@param type {String} 图形的种类
	 *		  操作类型:绘制曲线, 直线, 矩形, 椭圆
	 * 		  curve, line, rectangle, ellipse, clear
	 *	@param coords {Array} 绘图所需的坐标信息
	 */
	Note.Draw.prototype.drawShape = function(context, type, coords){
		switch(type){
			case "curve":{
				this._drawCurve(context, coords);
			}break;
			case "line":{
				this._drawLine(context, coords[0], coords[coords.length-1]);
			}break;
			case "rectangle":{
				this._drawRectangle(context, coords[0], coords[coords.length-1]);
			}break;
			case "ellipse":{
				this._drawEllipse(context, coords[0], coords[coords.length-1]);
			}break;
			default:{}break;
		}
	};
	
	/**
	 *	函数名: _drawLine
	 *	功能: 绘制直线
	 *	@param context {Object} 绘图句柄
	 *	@param start {x, y} 起点坐标
	 *	@param end {x, y} 终点坐标
	 */
	Note.Draw.prototype._drawLine = function(context, start, end){
		context.beginPath();
		context.moveTo(start.x, start.y);
		context.lineTo(end.x, end.y);
		context.strokeStyle = this._penStyle;
		context.lineWidth = this._penWidth;
		context.stroke();
	};
	
	/**
	 *	函数名: _drawCurve
	 *	功能: 绘制曲线
	 *	@param context {Object} 绘图句柄
	 *	@param coords {Object} 曲线上的坐标集合
	 */
	Note.Draw.prototype._drawCurve = function(context, coords){
		context.beginPath();
		context.moveTo(coords[0].x, coords[0].y);
		for(var i = 1; i < coords.length; i++){
			context.lineTo(coords[i].x, coords[i].y);
		}	
		context.strokeStyle = this._penStyle;
		context.lineWidth = this._penWidth;
		context.stroke();
	};
	
	/**
	 *	函数名: _drawRectangle
	 *	功能: 绘制 矩形
	 *	@param context {Object} 绘图句柄
	 *	@param start {left, top} 左上角坐标
	 *	@param end {right, buttom} 右下角坐标
	 */
	Note.Draw.prototype._drawRectangle = function(context, start, end){
		var width = Math.abs(start.x - end.x);
		var height = Math.abs(start.y - end.y);
		context.strokeStyle = this._penStyle;
		context.lineWidth = this._penWidth;
		context.strokeRect(Math.min(start.x, end.x), Math.min(start.y, end.y), width, height);
	};
	
	/**
	 *	函数名: _drawEllipse
	 *	功能: 绘制椭圆
	 *	@param context {Object} 绘图句柄
	 *	@param start {x, y} 左上角坐标
	 *	@param end {x, y} 右下角坐标
	 */
	Note.Draw.prototype._drawEllipse = function(context, start, end){
		var cx = (start.x + end.x)/2.0;
		var cy = (start.y + end.y)/2.0;
		var a = Math.abs(start.x - end.x);
		var b = Math.abs(start.y - end.y);
		var step = (a > b) ? 1.0 / a : 1.0 / b;
		context.beginPath();
		context.moveTo(cx + a, cy);
		for(var i = 0; i < 2 * Math.PI; i += step) {
			context.lineTo(cx + a * Math.cos(i), cy + b * Math.sin(i));
		}
		context.closePath();
		context.stroke();
	};
})();